﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ManguVariant
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;


        // maailmas leiduvad asjad
        Texture2D myTexture;
        Rectangle myRectangle;

       // tegelase kiirus
#region kiiruse muutujad
        int kiirusParemale = 0;
        int kiirusVasakule = 0;
        int kiirusUlesse = 0;
        int kiirusAlla = 0;
        int maxkiirus = 5;
        System.Random tegelane = new System.Random();
#endregion
        
#region ekraani mõõtmed
        // screeni laius
        int ekraaniLaius;
        int ekraaniKorgus;
        
#endregion 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            myTexture = Content.Load<Texture2D>("vaenlane");
            myRectangle = new Rectangle(50, 50, 100, 100);


            ekraaniLaius = GraphicsDevice.Viewport.Width;
            ekraaniKorgus = GraphicsDevice.Viewport.Height;

            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // Vaenlase liikumine
            #region Paremale vajutus
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                // Kasvatab kiirust, kuni jõuab max kiiruseni
                if (kiirusParemale < maxkiirus)
                {
                    // Kasvatab nupu vajutusega pikslite arvu, mida tegelane läbib.
                    kiirusParemale += 1;
                    myRectangle.X += kiirusParemale;

                }
                    // Kui kiirus ongi maksimum kiirus, siis edasi kasvatab pikslite vahemaad konstantse arvu võrra.
                else 
                {
                    myRectangle.X += maxkiirus;
                }

            }
                // Kui nupp lastakse lahti, hakkab seisma jääma
            else if (Keyboard.GetState().IsKeyDown(Keys.Right) == false)
            {
                // Kui nupp on lahti lastud, siis tegelane veel liigub. 
                // Seni kuni tegelane pole seisma jäänud, aeglustub ta kiirus 1 piksli võrra.

                if (kiirusParemale >= 0)
                {

                    kiirusParemale -= 1;
                    myRectangle.X += kiirusParemale;
                    
                }
            }
            #endregion

            #region Vasakule vajutus
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                   {

                       if (kiirusVasakule > -maxkiirus)
                          {
                             kiirusVasakule -= 1;
                                myRectangle.X += kiirusVasakule;
                          }
                          
                       else
                          {
                                    myRectangle.X -= maxkiirus;
                          }

                    }
             else if (Keyboard.GetState().IsKeyDown(Keys.Left) == false)
                    {
                                if (kiirusVasakule < 0)
                                {

                                    kiirusVasakule += 1;
                                    myRectangle.X += kiirusVasakule;
                                }
                    }

            #endregion

            #region ülesse vajutus
            if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
                if (kiirusUlesse > -maxkiirus)
                          {
                             kiirusUlesse -= 1;
                                myRectangle.Y += kiirusUlesse;
                          }
                          
                       else
                          {
                                    myRectangle.Y -= maxkiirus;
                          }

                    
             else if (Keyboard.GetState().IsKeyDown(Keys.Up) == false)
                    {
                                if (kiirusUlesse < 0)
                                {

                                    kiirusUlesse += 1;
                                    myRectangle.Y += kiirusUlesse;
                                }
                            }


            #endregion 
            if (Keyboard.GetState().IsKeyDown(Keys.Down))

            {
                if (kiirusAlla < maxkiirus)
                          {
                             kiirusAlla += 1;
                                myRectangle.Y += kiirusAlla;
                          }
                          
                       else
                          {
                                    myRectangle.Y += maxkiirus;
                          }

            }     
             else if (Keyboard.GetState().IsKeyDown(Keys.Down) == false)
                    {
                                if (kiirusAlla > 0)
                                {

                                    kiirusAlla -= 1;
                                    myRectangle.Y += kiirusAlla;
                                }
                    }

            

                            if (myRectangle.X <= 0)
                                myRectangle.X = 0;

                            if (myRectangle.X + myTexture.Width >= ekraaniLaius)
                                myRectangle.X = 0;

                            if (myRectangle.Y <= 0)
                                myRectangle.Y = 0;
                            if (myRectangle.Y + myTexture.Height >= ekraaniKorgus)
                                myRectangle.Y = ekraaniKorgus - myTexture.Height;

            





            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            
            spriteBatch.Begin();
            spriteBatch.Draw(myTexture, myRectangle, Color.White);
            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}
